package cn.tedu.rabbitmq.demo.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Slf4j
@Configuration
public class DeclareConfig {
    // 声明 队列 名字 demo-queue 持久化 不专属 不自动删除 没有特性属性
    // channel.queueDeclare("demo-queue",true,false,false,null);
    @Bean
    public Queue queue() {
        // 第一种写法 直接new Queue
        /*return new Queue("queue-demo", true, false, false, null);*/
        // 第二种 调用构建工具builder
        return QueueBuilder.nonDurable("demo-queue").withArgument("x-message-ttl",10000).build();
    }
    @Bean
    public Exchange declareFanoutExchange() {
        // 第一种写法 直接new
        /*return new FanoutExchange("demo-fanout");*/
        // 第二种写法 调用builder
        return ExchangeBuilder.fanoutExchange("demo-fanout").build();
    }
    // 绑定关系 Binding BindingBuilder 最后没有build() noargs()
    @Bean
    public Binding declareBinding() {
        // 第一种 new Binding();
        // 第二种 builder
        return BindingBuilder.
                bind(queue()).
                to(declareFanoutExchange()).
                with("*").noargs();
    }
    @Bean
    public Queue queue01() {
        return new Queue("demo-queue01", true, false, false, null);
    }
    @Bean
    public Exchange declareDirectExchange() {
        return ExchangeBuilder.directExchange("demo-direct").build();
    }
    @Bean
    public Binding declareBinding01() {
        return BindingBuilder.
                bind(queue01()).
                to(declareDirectExchange()).
                with("key01").noargs();
    }
    @Bean
    public Queue queue02() {
        return new Queue("demo-queue02", true, false, false, null);
    }
    @Bean
    public Binding declareBinding02() {
        return BindingBuilder.
                bind(queue02()).
                to(declareDirectExchange()).
                with("key02").noargs();
    }
}
